home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / nmbd_namequery.c.z / nmbd_namequery.c
Encoding:
C/C++ Source or Header  |  1998-10-28  |  8.4 KB  |  264 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    NBT netbios routines and daemon - version 2
  5.    Copyright (C) Andrew Tridgell 1994-1998
  6.    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
  7.    Copyright (C) Jeremy Allison 1994-1998
  8.    
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2 of the License, or
  12.    (at your option) any later version.
  13.    
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.    
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.    
  23. */
  24.  
  25. #include "includes.h"
  26.  
  27. extern int DEBUGLEVEL;
  28.  
  29. extern pstring scope;
  30.  
  31. /****************************************************************************
  32.  Deal with a response packet when querying a name.
  33. ****************************************************************************/
  34.  
  35. static void query_name_response(struct subnet_record *subrec,
  36.                        struct response_record *rrec, struct packet_struct *p)
  37. {
  38.   struct nmb_packet *nmb = &p->packet.nmb;
  39.   BOOL success = False;
  40.   struct nmb_name *question_name = &rrec->packet->packet.nmb.question.question_name;
  41.   struct in_addr answer_ip;
  42.  
  43.   /* Ensure we don't retry the query but leave the response record cleanup
  44.      to the timeout code. We may get more answer responses in which case
  45.      we should mark the name in conflict.. */
  46.   rrec->repeat_count = 0;
  47.  
  48.   if(rrec->num_msgs == 1)
  49.   {
  50.     /* This is the first response. */
  51.  
  52.     if(nmb->header.opcode == NMB_WACK_OPCODE)
  53.     {
  54.       /* WINS server is telling us to wait. Pretend we didn't get
  55.          the response but don't send out any more query requests. */
  56.   
  57.       DEBUG(5,("query_name_response: WACK from WINS server %s in querying \
  58. name %s on subnet %s.\n", inet_ntoa(p->ip), namestr(question_name), subrec->subnet_name));
  59.   
  60.       rrec->repeat_count = 0;
  61.       /* How long we should wait for. */
  62.       rrec->repeat_time = p->timestamp + nmb->answers->ttl;
  63.       rrec->num_msgs--;
  64.       return;
  65.     }
  66.     else if(nmb->header.rcode != 0)
  67.     {
  68.       success = False;
  69.  
  70.       DEBUG(5,("query_name_response: On subnet %s - negative response \
  71. from IP %s for name %s. Error code was %d.\n", subrec->subnet_name, inet_ntoa(p->ip), 
  72.                       namestr(question_name), nmb->header.rcode));
  73.     }
  74.     else
  75.     {
  76.       success = True;
  77.  
  78.       putip((char *)&answer_ip,&nmb->answers->rdata[2]);
  79.       DEBUG(5,("query_name_response: On subnet %s - positive response from IP %s \
  80. for name %s. IP of that name is %s\n", subrec->subnet_name, inet_ntoa(p->ip),
  81.                     namestr(question_name), inet_ntoa(answer_ip)));
  82.  
  83.       /* Interestingly, we could add these names to our namelists, and
  84.          change nmbd to a model that checked its own name cache first,
  85.          before sending out a query. This is a task for another day, though.
  86.        */
  87.     }
  88.   }
  89.   else if( rrec->num_msgs > 1)
  90.   {
  91.     DEBUG(0,("query_name_response: Multiple (%d) responses received for a query on \
  92. subnet %s for name %s. This response was from IP %s\n", 
  93.         rrec->num_msgs, subrec->subnet_name, namestr(question_name), 
  94.         inet_ntoa(p->ip) ));
  95.  
  96.     /* We have already called the success or fail function, so we
  97.        don't call again here. Leave the response record around in
  98.        case we get more responses. */
  99.  
  100.     return; 
  101.   }
  102.   
  103.   if(success && rrec->success_fn)
  104.     (*rrec->success_fn)(subrec, rrec->userdata, question_name, answer_ip, nmb->answers);
  105.   else if( rrec->fail_fn)
  106.     (*rrec->fail_fn)(subrec, rrec, question_name, nmb->header.rcode);
  107.  
  108. }
  109.  
  110. /****************************************************************************
  111.  Deal with a timeout when querying a name.
  112. ****************************************************************************/
  113.  
  114. static void query_name_timeout_response(struct subnet_record *subrec,
  115.                        struct response_record *rrec)
  116. {
  117.   struct nmb_packet *sent_nmb = &rrec->packet->packet.nmb;
  118.   /* We can only fail here, never succeed. */
  119.   BOOL failed = True;
  120.   struct nmb_name *question_name = &sent_nmb->question.question_name;
  121.  
  122.   if(rrec->num_msgs != 0)
  123.   {
  124.     /* We got at least one response, and have called the success/fail
  125.        function already. */
  126.  
  127.     failed = False; 
  128.   }
  129.  
  130.   if(failed)
  131.   {
  132.     DEBUG(5,("query_name_timeout_response: No response to querying name %s on subnet %s.\n",
  133.         namestr(question_name), subrec->subnet_name));
  134.  
  135.     if(rrec->fail_fn)
  136.       (*rrec->fail_fn)(subrec, rrec, question_name, 0);
  137.   }
  138.  
  139.   remove_response_record(subrec, rrec);
  140. }
  141.  
  142. /****************************************************************************
  143.  Lookup a name on our local namelists. We check the lmhosts file first. If the
  144.  name is not there we look for the name on the given subnet.
  145. ****************************************************************************/
  146.  
  147. static BOOL query_local_namelists(struct subnet_record *subrec, struct nmb_name *nmbname,
  148.                                   struct name_record **namerecp) 
  149. {
  150.   struct name_record *namerec;
  151.  
  152.   *namerecp = NULL;
  153.  
  154.   if(find_name_in_lmhosts(nmbname, namerecp))
  155.     return True;
  156.   
  157.   if((namerec = find_name_on_subnet(subrec, nmbname, FIND_ANY_NAME))==NULL)
  158.     return False;
  159.  
  160.   if(NAME_IS_ACTIVE(namerec) && ((namerec->source == SELF_NAME) || 
  161.                               (namerec->source == LMHOSTS_NAME)) )
  162.   {
  163.     *namerecp = namerec;
  164.     return True;
  165.   } 
  166.   return False;
  167. }
  168.  
  169. /****************************************************************************
  170.  Try and query for a name.
  171. ****************************************************************************/
  172.  
  173. BOOL query_name(struct subnet_record *subrec, char *name, int type,
  174.                    query_name_success_function success_fn,
  175.                    query_name_fail_function fail_fn, 
  176.                    struct userdata_struct *userdata)
  177. {
  178.   struct nmb_name nmbname;
  179.   struct name_record *namerec;
  180.  
  181.   make_nmb_name(&nmbname, name, type, scope);
  182.  
  183.   /*
  184.    * We need to check our local namelists first.
  185.    * It may be an magic name, lmhosts name or just
  186.    * a name we have registered.
  187.    */
  188.  
  189.   if(query_local_namelists(subrec, &nmbname, &namerec) == True)
  190.   {
  191.     struct res_rec rrec;
  192.     int i;
  193.  
  194.     bzero((char *)&rrec, sizeof(struct res_rec));
  195.  
  196.     /* Fake up the needed res_rec just in case it's used. */
  197.     rrec.rr_name = nmbname;
  198.     rrec.rr_type = RR_TYPE_NB;
  199.     rrec.rr_class = RR_CLASS_IN;
  200.     rrec.ttl = PERMANENT_TTL;
  201.     rrec.rdlength = namerec->num_ips * 6;
  202.     if(rrec.rdlength > MAX_DGRAM_SIZE)
  203.     {
  204.       DEBUG(0,("query_name: nmbd internal error - there are %d ip addresses for name %s.\n",
  205.                namerec->num_ips, namestr(&nmbname) ));
  206.       return False;
  207.     }
  208.  
  209.     for( i = 0; i < namerec->num_ips; i++)
  210.     {
  211.       set_nb_flags( &rrec.rdata[i*6], namerec->nb_flags );
  212.       putip( &rrec.rdata[(i*6) + 2], (char *)&namerec->ip[i]);
  213.     }
  214.  
  215.     /* Call the success function directly. */
  216.     if(success_fn)
  217.       (*success_fn)(subrec, userdata, &nmbname, namerec->ip[0], &rrec);
  218.     return False;
  219.   }
  220.  
  221.   if(queue_query_name( subrec,
  222.         query_name_response,
  223.         query_name_timeout_response,
  224.         success_fn,
  225.         fail_fn,
  226.         userdata,
  227.         &nmbname) == NULL)
  228.   {
  229.     DEBUG(0,("query_name: Failed to send packet trying to query name %s\n",
  230.           namestr(&nmbname)));
  231.     return True;
  232.   }
  233.   return False;
  234. }
  235.  
  236. /****************************************************************************
  237.  Try and query for a name from nmbd acting as a WINS server.
  238. ****************************************************************************/
  239.  
  240. BOOL query_name_from_wins_server(struct in_addr ip_to, 
  241.                    char *name, int type,
  242.                    query_name_success_function success_fn,
  243.                    query_name_fail_function fail_fn, 
  244.                    struct userdata_struct *userdata)
  245. {
  246.   struct nmb_name nmbname;
  247.  
  248.   make_nmb_name(&nmbname, name, type, scope);
  249.  
  250.   if(queue_query_name_from_wins_server( ip_to,
  251.         query_name_response,
  252.         query_name_timeout_response,
  253.         success_fn,
  254.         fail_fn,
  255.         userdata,
  256.         &nmbname) == NULL)
  257.   {
  258.     DEBUG(0,("query_name_from_wins_server: Failed to send packet trying to query name %s\n",
  259.           namestr(&nmbname)));
  260.     return True;
  261.   }
  262.   return False;
  263. }
  264.